//
//  SEFilterHeaderMenuTableView.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/19.
//

import UIKit

class SEFilterHeaderMenuTableView: UIView {
    
    var selectedClosure: ((IndexPath) -> Void)?
    
    private var style: SEFilterHeaderMenuViewStyle = .default
    private var addressModels: [AddressModel] = [] {
        didSet {
            tableView.reloadData()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    private func setupUI() {
        backgroundColor = .white
    }
    
    func reloadData(models: [AddressModel], style: SEFilterHeaderMenuViewStyle) {
        self.style = style
        self.addressModels = models
    }
    
    func selectRow(at indexPath: IndexPath? = nil) {
        if indexPath == nil {
            tableView.selectRow(at: IndexPath(row: 0, section: 0), animated: true, scrollPosition: .top)
        } else {
            tableView.selectRow(at: indexPath, animated: true, scrollPosition: .top)
        }
    }
    
    func scrollToTop() {
        tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        tableView.separatorStyle = .none
        addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }

    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.backgroundColor = .white
        tableView.showsVerticalScrollIndicator = false
        tableView.tableFooterView = UIView()
        tableView.estimatedRowHeight = 40
        tableView.rowHeight = UITableView.automaticDimension
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(SEFilterHeaderMenuCell.nibForCell(),
                           forCellReuseIdentifier: SEFilterHeaderMenuCell.reuseIdentifier())
        return tableView
    }()
}

extension SEFilterHeaderMenuTableView: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch style {
        case .default: return 0
        case .address: return addressModels.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SEFilterHeaderMenuCell.reuseIdentifier()) as! SEFilterHeaderMenuCell
        switch style {
        case .default: break
        case .address:
            cell.update(with: addressModels[indexPath.row], style: style)
        }
        return cell
    }
}

extension SEFilterHeaderMenuTableView: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedClosure?(indexPath)
    }
}
